package com.bp.api.channel;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.common.enums.CommonCode;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.common.enums.SystemStatusEnum;
import com.idp.biz.bpchannel.entity.BpChannel;
import com.idp.biz.bpchannel.service.BpChannelService;

/**
 * 频道
 */
@RestController
@RequestMapping(value = "/api/channel/", method = RequestMethod.POST)
public class ChannelController {
	private static Logger logger = LoggerFactory.getLogger(ChannelController.class);
	
	@Autowired
	BpChannelService channelService;
	
	/**
	 * 频道列表
	 */
	@CrossOrigin
	@RequestMapping("/list")
	public String list(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		try {
			BpChannel channel = new BpChannel();
			channel.setStatus(CommonCode.Y.getValue());
			channel.setSymbol(CommonCode.Y.getValue());
			List<BpChannel> dataList = channelService.findBySearch(channel);
			json.put("data", dataList);
			json.put("code", SystemStatusEnum.SUCCESS.getValue());
			json.put("message", SystemStatusEnum.SUCCESS.getName());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("code", SystemStatusEnum.SERVERERROR.getValue());
			json.put("message", SystemStatusEnum.SERVERERROR.getName());
		}
		return json.toString();
	}
	
	@CrossOrigin
	@RequestMapping("/demo")
	public String demo(HttpServletRequest request) {
		JSONObject json = new JSONObject();
		try {
			String phone = request.getParameter("phone");
			if(StringUtils.isEmpty(phone)){
				json.put("code", SystemStatusEnum.PARAMETER_CANNOT_BE_EMPTY.getValue());
				json.put("message", SystemStatusEnum.PARAMETER_CANNOT_BE_EMPTY.getName());
				return json.toString();
			}
			
			json.put("data", "");
			json.put("code", SystemStatusEnum.SUCCESS.getValue());
			json.put("message", SystemStatusEnum.SUCCESS.getName());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			json.put("code", SystemStatusEnum.SERVERERROR.getValue());
			json.put("message", SystemStatusEnum.SERVERERROR.getName());
		}
		return json.toString();
	}

}
